package com.easyShop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyShop.common.QueryPageParam;
import com.easyShop.common.Result;
import com.easyShop.entity.NextPage1;
import com.easyShop.service.NextPage1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  NextPage1 前端控制器
 * </p>
 *
 * @author wms
 * @since 2022-10-15
 */
@RestController
@RequestMapping("/nextPage1")
public class NextPage1Controller {

    @Autowired
    private NextPage1Service nextPage1Service;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody NextPage1 nextPage1){
        return nextPage1Service.save(nextPage1)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody NextPage1 nextPage1){
        return nextPage1Service.updateById(nextPage1)?Result.suc():Result.fail();
    }


    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return nextPage1Service.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage/{cid}")
    public Result listPage(@PathVariable long cid,@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Integer type = Integer.parseInt((String) param.get("type"));


        Page<NextPage1> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<NextPage1> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(NextPage1::getType,type);
        lambdaQueryWrapper.like(NextPage1::getProductAttributeCategoryId,cid);
        IPage<NextPage1> result = nextPage1Service.pageCC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}